package com.mbuswidget;

import java.util.ArrayList;
import java.util.Collections;

import android.content.SharedPreferences;
import android.location.Location;

public class MBusWidgetConstants {
	public static final String PREFS_NAME = "com.mbuswidget.MBusWidgetProvider";
	public static final String PREFS_PREX = "prefix_";
	public static final String PREFS_STOPLIST_SUFX = "stopList";
	public static final String PREFS_BLACKLIST_SUFX = "blacklist";
	public static final String PREFS_DELIMITER = ";";
	public static final String PREFS_WIDGET_IDS = "widgetids";
	public static final String PREFS_USE_LOCATION = "use_location";
	public static final String PREFS_WIDGET_STOP_INDEX = "widget_stop_index";
	public static final String INTENT_SELECTED_STOPS = "SELECTED_STOPS";
	public static final String SELECTED_ROUTE = "SELECTED_ROUTE";
	public static final String WIDGET_CREATED = "widget_created";
	public static final String WIDGETS_DELETED = "widget_deleted";
	public static final String MBUSLINK = "http://mbus.pts.umich.edu/shared/public_feed.xml";
	public static final String FORCE_SERVICE_RELOAD = "force_reload";
	public static final String SHOW_NEXT_STOP = "show_next_stop";
	public static final String SHOW_PREV_STOP = "show_prev_stop";
	public static final String CC_LITTLE = "Central Transit Center";
	public static final String IN_BOUND_SUFFEX = " In";
	public static final String OUT_BOUND_SUFFEX = " Out";
	public static final String STOP_NOT_SERVICED = "Stop Not Serviced";
	public static final String PRESS_RELOAD = "Press Reload";
	public static final String REFRESHING_WIDGET = "Refreshing...";
	public static final String DATA_LAST_RECIEVED_PREX = "Data ";
	public static final String DATA_LAST_RECIEVED_SUFX = " old";
	public static final String DATA_ERROR = "Connection Error";
	public static final String SCREEN_STATE_OFF = "screen_state_off";
	public static final String SCREEN_STATE_ON = "screen_state_on";
	public static final String DUPLICATED_STOPS = "duplicated_stops";
	public static final String LOCATION_UPDATED = "location_updated";
	public static final boolean USE_LOCATION_WIDGET_INIT = true;
	public static final String GPS_LOCATION_ENABLED = "gps_enabled";
	public static final String NETWORK_LOCATION_ENABLED = "network_enabled";
	public static final String FORCE_RESTART = "force_restart";
	public static final String DATA_IS_OLD = "No longer valid";
	public static final String BLACKLISTED_ROUTE_ADD = "blacked_route_add";
	public static final String BLACKLISTED_ROUTE_REMOVE = "blacked_route_remove";
	public static final boolean GPS_LOCATION_INIT_VALUE = false;
	public static final boolean NETWORK_LOCATION_INIT_VALUE = true;
	public static final String ERROR_CONNECTING_TO_SERVICE = "Error connecting to MBusWidget Service. Is the service running?";
	public static final int INVALID_ROUTE = -1;
	public static final int ADD_STOP = 1;
	public static final int REMOVE_STOP = 2;
	public static final String STOP_TYPE = "STOP_TYPE";
	public static final int GPS_TIME_INTERVAL = 2000; // milliseconds
	public static final float GPS_DIST_INTERVAL = 10; // meters
	public static final int INVALID_STOP_INDEX = 0;
	public static final int[] UpdateTaskInterval = { 1, 5, 10, 60 };
	public static final int[] UpdateTaskCutoffs = { 15, 30, 60, 600 };
	public static final String DOWNLOAD_INTERVAL = "download_interval";
	public static final int DOWNLOAD_INTERVAL_INIT = 15; // seconds
	public static final String DOWNLOAD_AUTO_ENABLED = "download_enabled";
	public static final boolean DOWNLOAD_AUTO_ENABLED_INIT = true;
	public static final String DUPLICATED_STOPS_INIT_LIST = "Bursley;Pierpont;Mitchell Field;FXB";
	public static final String DUPLICATED_STOPS_BLACKLIST = "duplicated_stops_blacklist";
	public static final String DUPLICATED_STOPS_BLACKLIST_INIT_LIST = "Central Transit Center";
	public static final String DOWNLOADING_NEW_DATA = "Retrieving Data";
	public static final String NO_DATA_AVALIBLE = "Service has not received data. Press Reload to try again.";
	public static final String MAX_ARRIVAL_TIME = "max_arrival_time";
	public static final String SERVICE_ANNOUNCE = "service_announce";
	public static final int MAX_ARRIVAL_TIME_INIT = 2;
	public static final double CC_LITTLE_LAT = 42.27818197456284;
	public static final double CC_LITTLE_LONG = -83.73513489961624;

	public static ArrayList<String> GetWidgetStopList(SharedPreferences pref,
			int widgetID) {
		String result = pref.getString(PREFS_PREX + widgetID
				+ PREFS_STOPLIST_SUFX, null);
		ArrayList<String> stopList = new ArrayList<String>();

		// If result is empty return
		if (result == null)
			return stopList;

		String[] splitResults = result.split(PREFS_DELIMITER);

		// Add all stops to the List
		Collections.addAll(stopList, splitResults);
		return stopList;
	}

	public static void SetWidgetStopList(SharedPreferences pref, int widgetID,
			ArrayList<String> stopList) {
		String saveString = null;
		if(stopList.size() != 0) saveString = "";
		for (String string : stopList) {
			saveString += (saveString.length() != 0 ? PREFS_DELIMITER : "") + string;
		}

		SharedPreferences.Editor editor = pref.edit();
		editor.putString(PREFS_PREX + widgetID + PREFS_STOPLIST_SUFX,
				saveString);
		editor.commit();
		// Done 
		return;
	}

	public static ArrayList<String> GetWidgetBlacklist(SharedPreferences pref,
			int widgetID) {
		String result = pref.getString(PREFS_PREX + widgetID
				+ PREFS_BLACKLIST_SUFX, null);
		ArrayList<String> blacklist = new ArrayList<String>();

		if (result == null)
			return blacklist;

		String[] splitResults = result.split(PREFS_DELIMITER);

		Collections.addAll(blacklist, splitResults);
		return blacklist;
	}

	public static void SetWidgetBlacklist(SharedPreferences pref, int widgetID,
			ArrayList<String> blacklist) {
		String saveString = null;
		if(blacklist.size() != 0) saveString = "";
		for (String string : blacklist) {
			saveString += (saveString.length() != 0 ? PREFS_DELIMITER : "") + string;
		}

		SharedPreferences.Editor editor = pref.edit();
		editor.putString(PREFS_PREX + widgetID + PREFS_BLACKLIST_SUFX,
				saveString);
		editor.commit();
		// Done
		return;
	}

	public static ArrayList<Integer> GetWidgetIDs(SharedPreferences pref) {
		String result = pref.getString(PREFS_PREX + PREFS_WIDGET_IDS, null);
		ArrayList<Integer> widgetIDs = new ArrayList<Integer>();

		if (result == null)
			return widgetIDs;

		String[] splitResults = result.split(PREFS_DELIMITER);

		for (String string : splitResults) {
			widgetIDs.add(Integer.parseInt(string));
		}
		return widgetIDs;
	}

	public static void SetWidgetIDs(SharedPreferences pref,
			ArrayList<Integer> widgetIds) {
		String saveString = null;
		if(widgetIds.size() != 0) saveString = "";
		for (Integer i : widgetIds) {
			saveString += (saveString.length() != 0 ? PREFS_DELIMITER : "") + i;
		}

		SharedPreferences.Editor editor = pref.edit();
		editor.putString(PREFS_PREX + PREFS_WIDGET_IDS, saveString);
		editor.commit();
	}

	public static boolean GetUseLocation(SharedPreferences pref, int widgetID) {
		return pref.getBoolean(PREFS_PREX + widgetID + PREFS_USE_LOCATION,
				USE_LOCATION_WIDGET_INIT);
	}

	public static boolean GetGPSLocationEnabled(SharedPreferences pref) {
		return pref.getBoolean(PREFS_PREX + GPS_LOCATION_ENABLED, GPS_LOCATION_INIT_VALUE);
	}

	public static void SetGPSLocationEnabled(SharedPreferences pref,
			boolean value) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(PREFS_PREX + GPS_LOCATION_ENABLED, value);
		editor.commit();
	}

	public static boolean GetNetworkLocationEnabled(SharedPreferences pref) {
		return pref.getBoolean(PREFS_PREX + NETWORK_LOCATION_ENABLED,
				NETWORK_LOCATION_INIT_VALUE);
	}

	public static void SetNetworkLocationEnabled(SharedPreferences pref,
			boolean value) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(PREFS_PREX + NETWORK_LOCATION_ENABLED, value);
		editor.commit();
	}

	public static void SetUseLocation(SharedPreferences pref, int widgetID,
			boolean status) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(PREFS_PREX + widgetID + PREFS_USE_LOCATION, status);
		editor.commit();
	}

	public static int GetStopIndex(SharedPreferences pref, int widgetID) {
		return pref.getInt(PREFS_PREX + widgetID + PREFS_WIDGET_STOP_INDEX,
				INVALID_STOP_INDEX);
	}

	public static void SetStopIndex(SharedPreferences pref, int widgetID,
			int index) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(PREFS_PREX + widgetID + PREFS_WIDGET_STOP_INDEX, index);
		editor.commit();
	}

	public static ArrayList<String> GetDuplicatedRoutes(SharedPreferences pref) {
		String result = pref.getString(PREFS_PREX + DUPLICATED_STOPS,
				DUPLICATED_STOPS_INIT_LIST);
		ArrayList<String> dupStops = new ArrayList<String>();

		if (result == null)
			return dupStops;

		String[] splitResults = result.split(PREFS_DELIMITER);

		for (String string : splitResults) {
			dupStops.add(string);
		}
		return dupStops;
	}

	public static void SetDuplicatedRoutes(SharedPreferences pref,
			ArrayList<String> dupStops) {
		String saveString = null;
		if(dupStops.size() != 0 ) saveString = "";
		for (String string : dupStops) {
			saveString += (saveString.length() != 0 ? PREFS_DELIMITER : "") + string;
		}

		SharedPreferences.Editor editor = pref.edit();
		editor.putString(PREFS_PREX + DUPLICATED_STOPS, saveString);
		editor.commit();
		// Done
		return;
	}

	public static Location GetCCLittleLoc() {
		Location ccLittleLoc = new Location("mbuswidget");
		ccLittleLoc.setLatitude(CC_LITTLE_LAT);
		ccLittleLoc.setLongitude(CC_LITTLE_LONG);
		return ccLittleLoc;
	}
	
	public static int GetDownloadInterval(SharedPreferences pref) {
		return pref.getInt(PREFS_PREX + DOWNLOAD_INTERVAL, DOWNLOAD_INTERVAL_INIT);
	}
	
	public static void SetDownloadInterval(SharedPreferences pref,
			int time) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(PREFS_PREX + DOWNLOAD_INTERVAL, time);
		editor.commit();
	}
	
	public static boolean GetDownloadIntervalEnabled(SharedPreferences pref) {
		return pref.getBoolean(PREFS_PREX + DOWNLOAD_AUTO_ENABLED, DOWNLOAD_AUTO_ENABLED_INIT);
	}
	
	public static void SetDownloadIntervalEnabled(SharedPreferences pref,
			boolean value) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(PREFS_PREX + DOWNLOAD_AUTO_ENABLED, value);
		editor.commit();
	}
	
	public static Integer GetMaxArrivalTimes(SharedPreferences pref, int widgetId) {
		return pref.getInt(PREFS_PREX + widgetId + MAX_ARRIVAL_TIME, MAX_ARRIVAL_TIME_INIT);
	}
	
	public static void SetMaxArrivalTimes(SharedPreferences pref, int widgetId, int maxArrivalTime) {
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(PREFS_PREX + widgetId + MAX_ARRIVAL_TIME, maxArrivalTime);
		editor.commit();
	}
	
	public static ArrayList<String> GetDuplicatedRoutesBlacklist(SharedPreferences pref) {
		String result = pref.getString(PREFS_PREX + DUPLICATED_STOPS_BLACKLIST,
				DUPLICATED_STOPS_BLACKLIST_INIT_LIST);
		ArrayList<String> dupStops = new ArrayList<String>();

		if (result == null)
			return dupStops;

		String[] splitResults = result.split(PREFS_DELIMITER);

		for (String string : splitResults) {
			dupStops.add(string);
		}
		return dupStops;
	}

	public static void SetDuplicatedRoutesBlacklist(SharedPreferences pref,
			ArrayList<String> dupStops) {
		String saveString = null;
		if(dupStops.size() != 0 ) saveString = "";
		for (String string : dupStops) {
			saveString += (saveString.length() != 0 ? PREFS_DELIMITER : "") + string;
		}

		SharedPreferences.Editor editor = pref.edit();
		editor.putString(PREFS_PREX + DUPLICATED_STOPS_BLACKLIST, saveString);
		editor.commit();
		// Done
		return;
	}
}
